let express = require('express');
let router = express.Router();
let studentsDao = require("../dao/StudentsDao");

let pager = require("../util/PagerHelper");

/* GET users listing. */
router.all('/list.do', function(req, res, next) {
  // pager.href = "/api/students/list.do";
  let where = "";
  let flag = 0;//控制多条件是否要加and
  if (req.body) {
    // console.log("req.body", req.body);
    if (req.body.sname) {
      if (where.trim().length === 0) {
        where += "where ";
      }
      where += ` sname like '%${req.body.sname}%' `;
      flag++;
    }
    if (req.body.phone) {
      if (where.trim().length === 0) {
        where += "where ";
      }
      if (flag > 0) {
        where += " and "
      }
      where += ` phone like '%${req.body.phone}%' `;
      flag++;
    }
    if (req.body.gender) {
      if (where.trim().length === 0) {
        where += "where ";
      }
      if (flag > 0) {
        where += " and "
      }
      where += ` gender = '${req.body.gender}' `;
      flag++;
    }
    if (req.body.dormId) {
      if (where.trim().length === 0) {
        where += "where ";
      }
      if (flag > 0) {
        where += " and "
      }
      where += ` dormId = '${req.body.dormId}' `;
      flag++;
    }
    console.log(where);
  }
  studentsDao.getCount(where).then(count => {
    pager.setCount(count);
    return studentsDao.findByPage(where);
  }).then(data => {
    pager.data = data;
    res.json({
      code: 200, //结果编码，200：成功，404：未找到等
      data: pager
    });
  }).catch(err => next(err));
});

/**
 * 验证code是否存在
 */
router.get("/checkCode.do", ((req, res, next) => {
  if (req.query.code) {
    studentsDao.findByCode(req.query.code, req.query.id).then(data => {
      if (data) {
        res.json({
          code: 200,
          msg: true
        })
      } else {
        res.json({
          code: 200,
          msg: false
        })
      }
    }).catch(err => next(err));
  } else {
    res.json({
      code: 404,
      msg: "没有传学号code值"
    })
  }
}));

/**
 * 根据id删除一条数据
 */
router.get("/del.do", ((req, res, next) => {
  if (req.query.id) {
    studentsDao.deleteById(req.query.id).then(data => {
      res.json({
        code: 200,
        msg: "删除成功"
      })
    }).catch(err => next(err));
  } else {
    res.json({
      code: 404,
      msg: "没有传id值"
    })
  }
}));

/**
 * 根据ids删除多条数据
 */
router.get("/delBatch.do", (req, res, next) => {
  let idsStr = req.query.ids;
  console.log(idsStr);
  let ids;
  if (Array.isArray(idsStr)) {
    ids = idsStr;
  } else {
    ids = idsStr.split(",");//得到的是一个字符串数组
  }

  studentsDao.deleteByIds(ids).then(data => res.json({
    code: 200,
    msg: "删除成功"
  })).catch(err => next(err));
});

/**
 * 编辑功能
 */
router.get("/edit.do", (req, res, next) => {
  let {id} = req.query;
  if (id) {
    // let editObj = books.find(value => value.id == id);
    studentsDao.findById(id).then(editObj => {
      if (editObj) {
        res.json({
          code: 200,
          data: editObj
        });
      } else {
        res.json({
          code: 404,
          msg: "没有找到记录！"
        });
      }
    }).catch(err => next(err));

  } else {
    res.json({
      code: 404,
      msg: "没有id！"
    });
  }

});

/**
 * 保存功能
 */
router.post("/save.do", (req, res, next) => {
  let newObj = Object.assign({}, req.body);
  //区分添加还是编辑
  if (newObj.id) {
    //编辑
    studentsDao.update(newObj, newObj.id).then(data => res.json({
      code: 200,
      msg: "修改成功"
    })).catch(err => next(err));
  } else {
    //添加
    newObj.id = 0;//id是自动增长，必须是0值
    studentsDao.insert(newObj).then(data => res.json({
      code: 200,
      msg: "添加成功"
    })).catch(err => next(err));
  }
});

module.exports = router;
